package GUI.calendar;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Random;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import GUI.main.dialogs.AddAppointmentDialog;



public class CalendarGridPanel extends JPanel implements MouseListener {

	private static final long serialVersionUID = 1L;
	private static final Color OFFICE_BLUE = new Color(40, 100, 220);
	private static final Random rand = new Random();
	
	private int globalDayOffset;

	// globalDayOffset is the number of days between the first day of this month and Jan 1, 2011
	public CalendarGridPanel(int firstDayOfMonth, int daysInMonth, int globalDayOffset) {
		reset(firstDayOfMonth, daysInMonth, globalDayOffset);
	}
	
	public void reset(int firstDayOfMonth, int daysInMonth, int globalDayOffset) {
		this.globalDayOffset = globalDayOffset;
		
		removeAll();

		int rows = (firstDayOfMonth + daysInMonth) / 7 + 2;
		
		setLayout(new GridLayout(rows, 7));
		setBorder(BorderFactory.createLineBorder(Color.BLACK, 4));
		
		// add the 7 day labels
		add(new CalendarGridDayLabelPanel("SUN"));
		add(new CalendarGridDayLabelPanel("MON"));
		add(new CalendarGridDayLabelPanel("TUE"));
		add(new CalendarGridDayLabelPanel("WED"));
		add(new CalendarGridDayLabelPanel("THU"));
		add(new CalendarGridDayLabelPanel("FRI"));
		add(new CalendarGridDayLabelPanel("SAT"));
		
		// add the actual days
		boolean exists = false;
		for (int day = 0; day < (rows - 1) * 7; day++) {
			exists = day >= firstDayOfMonth && day < firstDayOfMonth + daysInMonth;
			CalendarGridCellPanel cell = new CalendarGridCellPanel(day - firstDayOfMonth,
					exists,
					rand.nextDouble() > 0.8);
			if (exists) {
				cell.addMouseListener(this);
			}
			add(cell);
		}
	}
	
	
	
	private class CalendarGridDayLabelPanel extends JPanel {
		
		private static final long serialVersionUID = 1L;
		
		private JLabel text;
		
		public CalendarGridDayLabelPanel(String label) {
			setLayout(new GridBagLayout());
			setBackground(OFFICE_BLUE);
			setBorder(BorderFactory.createMatteBorder(0, 0, 0, 2, Color.WHITE));
			
			text = new JLabel(label);
			text.setForeground(Color.WHITE);
			add(text);
		}
		
		public void paintComponent(Graphics g) {
			super.paintComponent(g);
			int fontSize = Math.min(Math.min(30, getHeight() - 4), getWidth() / 3);
			Font dayLabelFont = new Font(Font.SANS_SERIF, Font.BOLD, fontSize);
			text.setFont(dayLabelFont);
		}
		
	}


	
	public void mouseReleased(MouseEvent e) {
		if (e.getClickCount() == 2) {
			int globalDayCount = globalDayOffset
					+ ((CalendarGridCellPanel) e.getSource()).getDayOfMonth();
			AddAppointmentDialog aad = new AddAppointmentDialog();
			aad.setVisible(true);
			//JOptionPane.showMessageDialog(this, "DAY: " + globalDayCount + "!");
		}
	}

	public void mouseClicked(MouseEvent e) {}
	public void mousePressed(MouseEvent e) {}
	public void mouseEntered(MouseEvent e) {}
	public void mouseExited(MouseEvent e) {}
}
